package com.truongnha.clienttn.business.service;

import java.util.List;

import android.content.Context;
import android.util.Log;

import com.truongnha.clienttn.business.model.ASubjectMarkModel;
import com.truongnha.clienttn.data.dao.CreateDatabase;
import com.truongnha.clienttn.data.dao.StudentMarkDAO;
import com.truongnha.clienttn.data.dao.StudentSubjectDAO;
import com.truongnha.clienttn.data.entity.StudentMarkEntity;
import com.truongnha.clienttn.data.entity.StudentSubjectEntity;
import com.truongnha.clienttn.data.sao.StudentMarkSAO;

public class StudentMarkService {
	Context context;

	public StudentMarkService(Context context) {
		super();
		this.context = context;
	}

	public void getMarkFromServer() {
		StudentMarkSAO studentMarkSAO = new StudentMarkSAO(context);
		studentMarkSAO.getMarkFromServer();
	}

	public void getMarkFromData(List<ASubjectMarkModel> list) {
		try {
			CreateDatabase db = new CreateDatabase(context);
			// get list subject
			StudentSubjectDAO studentSubjectDAO = new StudentSubjectDAO(
					db.open());
			List<StudentSubjectEntity> listStudentSubject = studentSubjectDAO
					.getAll();
			for (StudentSubjectEntity studentSubjectEntity : listStudentSubject) {
				ASubjectMarkModel aSubjectMarkModel = new ASubjectMarkModel(
						studentSubjectEntity.getMarkId(),
						studentSubjectEntity.getName());
				list.add(aSubjectMarkModel);
			}
			db.close();
		} catch (Exception e) {
			Log.e("log error","error in student mark service 1" + e.toString() + e.getStackTrace().toString());
		}
		try {
			// put mark into list
			CreateDatabase db1 = new CreateDatabase(context);
			StudentMarkDAO studentMarkDAO = new StudentMarkDAO(db1.open());
			List<StudentMarkEntity> listMarkEntity = studentMarkDAO.getAll();
			for (StudentMarkEntity studentMarkEntity : listMarkEntity) {
				for (ASubjectMarkModel aSubjectMarkModel : list) {
					if (aSubjectMarkModel.getMarkId() == studentMarkEntity
							.getMark_id()) {
						aSubjectMarkModel.add(studentMarkEntity);
						break;
					}
				}
			}

			db1.close();
		} catch (Exception e) {
			Log.e("log error","error in student mark service 2 " + e.toString() + e.getStackTrace().toString());
		}
	}
}
